package mobile.TheOldRepublic.video;

import mobile.TheOldRepublic.MediaView;
import mobile.TheOldRepublic.R;
import mobile.TheOldRepublic.util.Constants;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class VideoPlayer extends Activity implements SurfaceHolder.Callback, OnErrorListener {
	
	public static final String SHARED_PREF_NAME = "VideoPlayer";
	
	private static final String DOWNLOADING_VIDEO_TEXT = "Video cannot be streamed, downloading now.";
		
	protected String videoUrl;
	private boolean errorHandled = false;
	protected MediaController mc = null;
	protected VideoView videoView = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Bundle extras = getIntent().getExtras();
		videoUrl = extras.getString(Constants.KEY_URL);
		setContentView(R.layout.video_layout);
		videoView = (VideoView)findViewById(R.id.vp_view);
		getWindow().setFormat(PixelFormat.TRANSLUCENT);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		
		openVideo();
	}
	
	private void openVideo(){
		try{
			MediaController mc = new MediaController(this);
			videoView.setMediaController(mc);
			Log.d(getClass().getSimpleName(), "Opening "+videoUrl);
			videoView.setVideoURI(Uri.parse(videoUrl));
			videoView.requestFocus();
			videoView.start();
		}catch(Exception e){
			e.printStackTrace();
			Toast.makeText(this, "Unable to start video.", Toast.LENGTH_LONG).show();
			this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
		}
	}
	
	@Override
	public void onResume(){
		super.onResume();
		if(videoView == null) return;
		
		Toast.makeText(this, Constants.STREAM_VIDEO_TEXT, Toast.LENGTH_SHORT).show();
		SharedPreferences prefs = this.getSharedPreferences(SHARED_PREF_NAME, MODE_WORLD_READABLE);
		if(prefs.contains(videoUrl)){
			videoView.seekTo(prefs.getInt(videoUrl, 0));
		}
		videoView.start();
	}
	
	@Override
	public void onPause(){
		super.onPause();
		SharedPreferences.Editor prefs = this.getSharedPreferences(SHARED_PREF_NAME, MODE_WORLD_WRITEABLE).edit();
		prefs.putInt(videoUrl, videoView.getCurrentPosition());
		prefs.commit();
	}	

	@Override
	public void onStop() {
		super.onStop();
		/*if(internalStorage){
			// TODO add processing for cleaning up internal storage
			outputFile.deleteOnExit();
		}*/
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		Log.d(this.getClass().getSimpleName(), "Surface has been created.");
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		if(videoView != null){
			videoView.stopPlayback();
			videoView.clearFocus();
		}
	}

	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		Log.i(this.getClass().getSimpleName(), what+","+extra);
		/*if(!errorHandled)
		try{
			errorHandled = true;
			mp.prepare();
			mp.start();
		}catch(Exception e){
			e.printStackTrace();
		}*/
		return errorHandled;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event){
		if(keyCode == KeyEvent.KEYCODE_BACK){
			Log.d(this.getClass().getSimpleName(), "Pressing back button.");
			if(videoView != null){
				videoView.stopPlayback();
				videoView.clearFocus();
			}
			
            Intent myIntent = new Intent(this, MediaView.class);
            this.startActivity(myIntent);
		}
		return super.onKeyDown(keyCode, event);
	}
}
